package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-subarray/
 * @create 2025/8/29 16:40
 * @since JDK17
 */

public class Solution20 {
    public int maxSubArray(int[] nums) {
        int n = nums.length, ans = Math.max(nums[0], 0);
        int[] dp = new int[n];
        dp[0] = nums[0];
        for (int i = 1; i < n; i++) {
            dp[i] = nums[i] + Math.max(0, dp[i - 1]);
            ans = Math.max(dp[i], ans);
        }
        return ans;
    }
}
